﻿using System.Collections.Generic;
using System.Windows.Forms;
using MediaController.Client.Controls.Dialogs;
using MediaController.Configuration;
using MediaController.Configuration.Interfaces;
using MediaController.Client.DAL;

namespace MediaController.Client.Controls.UserControls
{
    public partial class WatchFolders : UserControl
    {
        public WatchFolders()
        {
            InitializeComponent();
            _bindingSource.DataSource = DataAccess.Instance.MediaControllerDataSet;
            if (_dataGridViewFolders.Rows.Count > 0)
            {
                _bindingSource.MoveFirst();
            }
            InitGridView();
        }

        private void InitGridView()
        {
            idDataGridViewTextBoxColumn.Visible = false;
            filterDataGridViewTextBoxColumn.Visible = false;
        }

        private void ButtonEditClick(object sender, System.EventArgs e)
        {
            if (_dataGridViewFolders.SelectedRows.Count > 0)
            {
                SelectFolderDlg selectFolderDlg = new SelectFolderDlg();
                selectFolderDlg.Path = _dataGridViewFolders.SelectedRows[0].Cells["sourceDataGridViewTextBoxColumn"].Value.ToString();
                if (selectFolderDlg.ShowDialog() == DialogResult.OK)
                {
                    _dataGridViewFolders.SelectedRows[0].Cells["sourceDataGridViewTextBoxColumn"].Value = selectFolderDlg.Path;
                }
            }
        }

        private void ButtonAddClick(object sender, System.EventArgs e)
        {
            SelectFolderDlg selectFolderDlg = new SelectFolderDlg();
            //FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (selectFolderDlg.ShowDialog() == DialogResult.OK)
            {
                var listenersRow = DataAccess.Instance.MediaControllerDataSet.Listeners.NewListenersRow();
                
                listenersRow.Source = selectFolderDlg.Path;
                listenersRow.Filter = "*.rar;*.001";
                listenersRow.Move = true;
                listenersRow.Execute = "";
                listenersRow.Enabled = true;
                DataAccess.Instance.MediaControllerDataSet.Listeners.AddListenersRow(listenersRow);
                DataAccess.Instance.MediaControllerDataSet.Listeners.AcceptChanges();
            }
        }

        private void DataGridViewFoldersSelectionChanged(object sender, System.EventArgs e)
        {
            if (_dataGridViewFolders.SelectedRows.Count > 0)
            {
                _buttonEdit.Enabled = true;
                _buttonDelete.Enabled = true;
            }
        }

        private void _dataGridViewFolders_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(_dataGridViewFolders.Columns[e.ColumnIndex].HeaderText == "Source")
            {
                SelectFolderDlg selectFolderDlg = new SelectFolderDlg();
                selectFolderDlg.Path = _dataGridViewFolders.SelectedRows[0].Cells["sourceDataGridViewTextBoxColumn"].Value.ToString();
                if (selectFolderDlg.ShowDialog() == DialogResult.OK)
                {
                    _dataGridViewFolders.SelectedRows[0].Cells["sourceDataGridViewTextBoxColumn"].Value = selectFolderDlg.Path;
                }
            }
        }
    }
}